/*
 *  AudioFrameBuffer.h
 *  MantuPlay
 *
 *  Created by Markus Sintonen on 3.11.2009.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#define Error(Str)   fprintf(stderr, "%s\n", Str)
#define FatalError(Str)   fprintf(stderr, "%s\n", Str), exit(1)

class AudioFrameBuffer {
private:
	unsigned int currentFramePosition;
	unsigned int frameCount;
	SInt32 *frameData;
		
public:
	AudioFrameBuffer(unsigned int numberOfFrames)
	{
		currentFramePosition = 0;
		frameCount = numberOfFrames;
		frameData = new SInt32[numberOfFrames];
	}
	~AudioFrameBuffer() 
	{
		delete[] frameData;
		frameData = NULL;
	}
	
	unsigned int SetFrames(SInt32 *frames, unsigned int count)
	{
		if(count <= 0) {
			return 0;
		}
		
		if(count != frameCount) {
			delete frameData;
			frameData = NULL;
			frameData = new SInt32[count];
		}
		
		currentFramePosition = 0;
		frameCount = count;
		
		memcpy((void*)frameData, (void*)frames, count * sizeof(SInt32));
		return frameCount;
	}
	
	unsigned int NextFrames(SInt32 *framesDest, unsigned int count)
	{
		if(currentFramePosition == frameCount)
			return 0;
		
		unsigned int frameCountToGet = count;
		if((frameCountToGet + currentFramePosition) > frameCount) {
			frameCountToGet = frameCount - currentFramePosition;
		}
				
		memcpy(framesDest, &frameData[currentFramePosition], frameCountToGet * sizeof(SInt32));
		currentFramePosition += frameCountToGet;
		return frameCountToGet;
	}
};